const mongo = require('mongodb');
const ObjectID = mongo.ObjectID;
const qiniu = require('yi-qiniu');

module.exports = {
  description:'获取七牛上传token令牌',
  handler: (req, res, next) => {
    let id = new ObjectID();
    let body = req.body;
    let callbackBody = {
      _id: id,
      filename: body.filename,
      projectId: body.projectId,
      projectName: body.projectName,
      fileType: body.fileType,
      userId: body.userId,
      userName: body.userName,
      md5: body.md5,
      filesize: body.size,
      companyId: body.companyId,
      static: 2
    };
    let callbackUrl = 'http://127.0.0.1:8084/file/qiniucb';
    callbackUrl = req.config.sys.qiniuCallback || callbackUrl;
    qiniu.getUploadToken(callbackBody, callbackUrl, (e, r) => {
      res.reply({ code: 0, message: '获取uploadToken成功', data: r });
      req.mongo.collection('file').insertOne(callbackBody, (e, r) => { });
    });
  }
};